#include"BlockQueue.hpp"
#include"Task.hpp"
#include<unistd.h>

struct Data
{
    BlockQueue<Task>* bq;
};

void* consumer(void* args)
{
    Data* d = static_cast<Data*>(args);
    while(1)
    {
        sleep(1);
        Task t;
        d->bq->Pop(&t);
        t();
        t.Print();
    }
    return (void*)0;
}

void* productor(void* args)
{
    Data* d = static_cast<Data*>(args);
    int data = 1;
    while(1)
    {
        int x = data;
        int y = data + 1;
        Task t(x, y);
        data++;
        d->bq->Enqueue(t);
        std::cout << "生产了一个任务" << std::endl;
    }
    return (void*)0;
}

int main()
{
    BlockQueue<Task>* bq = new BlockQueue<Task>();
    pthread_t c, p;
    Data c_data = { bq };
    pthread_create(&c, nullptr, consumer, (void*)&c_data);
    Data p_data = { bq };
    pthread_create(&p, nullptr, productor, (void*)&p_data);
    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    return 0;
}